package com.dahuyou.netty.keepaliveAndIdle.client;

import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.timeout.IdleStateEvent;

import java.text.SimpleDateFormat;
import java.util.Date;

public class MyClientHandler extends ChannelDuplexHandler {

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        System.out.println("MyClientHandler.userEventTriggered");
        // 如果是触发了write的idle则发送keepalive消息，防止server端因为自己idle了而断开连接
        if (evt == IdleStateEvent.FIRST_WRITER_IDLE_STATE_EVENT) {
            System.out.println("client write idle");
            ctx.writeAndFlush("hi server, this is keepalive message from client, i am still there,do not close me!\r\n");
        }
        super.userEventTriggered(ctx, evt);
    }
}
